<?php
namespace app\school\controller\sys_admin;
use app\AdminController;

use app\school\model\TopicCourseModel;
use think\Db;

/**
 * 专题课程
 */
class TopicCourse extends AdminController
{
	/*------------------------------------------------------ */
	//-- 优先执行
	/*------------------------------------------------------ */
	public function initialize(){
        parent::initialize();
        $this->Model = new TopicCourseModel();
    }
	/*------------------------------------------------------ */
	//-- 主页
	/*------------------------------------------------------ */
    public function index(){
        $this->getList(true);
		return $this->fetch();
	}
    /*------------------------------------------------------ */
    //-- 获取列表
    //-- $runData boolean 是否返回模板
    /*------------------------------------------------------ */
    public function getList($runData = false)
    {
        $where = [];
        $search['keyword'] = input('keyword', '', 'trim');
        if (empty($search['keyword']) == false) $where[] = ['title', 'like', '%' . $search['keyword'] . '%'];
        $data = $this->getPageList($this->Model, $where);
        $this->assign("data", $data);
        $this->assign("search", $search);
        if ($runData == false) {
            $data['content'] = $this->fetch('list')->getContent();
            unset($data['list']);
            return $this->success('', '', $data);
        }
        return true;
    }
    /*------------------------------------------------------ */
    //-- 验证数据
    /*------------------------------------------------------ */
    private function checkData($data) {
        if (empty($data['title_img'])){
            $this->error('请上传标题图片.');
        }
        if (count($data['user_auth'])>0) {
            $data['user_auth'] = implode(',', $data['user_auth']);
        }
        if (empty($data['video_url']) == false){
            $video_url = $data['video_url'];
            $file_path = config('config._upload_').'school/video/';
            if ($data['id'] > 0){
                $oldRow = $this->Model->where('id',$data['id'])->find();
                if ($oldRow['video_url'] != $video_url){
                    $data['video_url'] = copyFile($video_url,$file_path);
                    @unlink('.'.$oldRow['video_url']);
                    @unlink('.'.$video_url);
                }
            }else{
                $data['video_url'] = copyFile($video_url,$file_path);
                @unlink('.'.$video_url);
            }
        }
        return $data;
    }
    /*------------------------------------------------------ */
    //-- 添加前调用
    /*------------------------------------------------------ */
    public function beforeAdd($data)
    {
        $data['add_time'] = time();
        return $this->checkData($data);
    }
    //-- 添加后调用
    /*------------------------------------------------------ */
    public function afterAdd($data)
    {
        return $this->success('添加成功.', url('index'));
    }
    /*------------------------------------------------------ */
    //-- 修改前调用
    /*------------------------------------------------------ */
    public function beforeEdit($data)
    {
        return $this->checkData($data);
    }
    /*------------------------------------------------------ */
    //-- 修改后调用
    /*------------------------------------------------------ */
    public function afterEdit($data)
    {
        return $this->success('修改成功.');
    }
    /*------------------------------------------------------ */
    //-- 删除
    /*------------------------------------------------------ */
    public function del()
    {
        $map['id'] = input('id', 0, 'intval');
        if ($map['id'] < 1) return $this->error('传递参数失败！');
        $row = $this->Model->where($map)->find();
        $res = $this->Model->where($map)->delete();
        if ($res < 1) return $this->error();
        @unlink('.'.$row['video_url']);
        return $this->success('删除成功.');
    }
    /*------------------------------------------------------ */
    //-- 添加/修改
    /*------------------------------------------------------ */
    public function info()
    {
        $pk = $this->Model->pk;
        if ($this->request->isPost()) {
            if (false === $data = $_POST) {
                $this->error($this->Model->getError());
            }
            if (empty($data[$pk])) {
                $this->_priv($this->module, $this->controller, 'add');
                if (method_exists($this, 'beforeAdd')) {
                    $data = $this->beforeAdd($data);
                }
                unset($data[$pk]);
                $res = $this->Model->allowField(true)->save($data);
                if ($res > 0) {
                    $data[$pk] = $this->Model->$pk;
                    if (method_exists($this->Model, 'cleanMemcache')) $this->Model->cleanMemcache($res);
                    if (method_exists($this, 'afterAdd')) {
                        $result = $this->afterAdd($data);
                        if (is_array($result)) return $this->ajaxReturn($result);
                    }
                    return $this->success('添加成功.', url('index'));
                }
            } else {
                $this->_priv($this->module, $this->controller, 'edit');
                if (method_exists($this, 'beforeEdit')) {
                    $data = $this->beforeEdit($data);
                }
                $res = $this->Model->allowField(true)->save($data, $data[$pk]);
                if ($res > 0) {
                    if (method_exists($this->Model, 'cleanMemcache')) $this->Model->cleanMemcache($data[$pk]);
                    if (method_exists($this, 'afterEdit')) {
                        $result = $this->afterEdit($data);
                        if (is_array($result)) return $this->ajaxReturn($result);
                    }
                    return $this->success('修改成功.', url('index'));
                }
            }
            return $this->error('操作失败.');
        }

        $id = input($pk, 0, 'intval');
        $row = ($id == 0) ? $this->Model->getField() : $this->Model->find($id);
        //代理商等级
        $proxy_data=Db::name('channel_proxy_level')->order('proxy_id desc')->field('proxy_id,proxy_name')->select();
        //普通用户
        $data_infos=array(array('proxy_id'=>0,'proxy_name'=>"普通用户"));

        $user_data=Db::name('distribution_dividend_role')->field('role_id as proxy_id,role_name as proxy_name')->select();
        $dats=array_merge($user_data,$proxy_data);
        $dats=array_merge($data_infos,$dats);


        if ($id > 0 && empty($row) == false) {
            $row = $row->toArray();
            if (!empty($row['user_auth'])) {
                $row['user_auth'] = explode(",",$row['user_auth']);
            }
        }
        if (method_exists($this, 'asInfo')) {
            $row = $this->asInfo($row);
        }

        $this->assign("row", $row);
        $this->assign("user_role", $dats);
        $ishtml = input('ishtml', 0, 'intval');
        if ($this->request->isAjax() && $ishtml == 0) {
            $result['code'] = 1;
            $result['data'] = $this->fetch('info')->getContent();
            return $this->ajaxReturn($result);
        }

        return $this->fetch('info');

    }
    /*------------------------------------------------------ */


}
